using System;
using UnityEngine;

public class MarketPriceTag : MonoBehaviour
{
	[Header("Ценник и изображение валюты без скидки")]
	public UILabel priceLabel;

	public UISprite cashIcon;

	public UISprite goldIcon;

	[Header("Контейнер для этого дела")]
	public GameObject holder;

	[Header("Ценник и изображение валюты со скидкой")]
	public UILabel saleOldPriceLabel;

	public UISprite saleOldCashIcon;

	public UISprite saleOldGoldIcon;

	public UISprite RedLineSprite;

	public UILabel saleNewPriceLabel;

	public UISprite saleNewCashIcon;

	public UISprite saleNewGoldIcon;

	[Header("Контейнер для этого дела")]
	public GameObject saleHolder;

	[Header("Показывать ли цену по центру")]
	public bool forceShowOldStylePrice;

	[Header("Лейбл \"For free\"")]
	public GameObject freeLabel;

	private Product currentProduct;

	private int startFontSize;

	private void Awake()
	{
		startFontSize = priceLabel.fontSize;
	}

	private void OnEnable()
	{
		if (currentProduct != null)
		{
			SetPrice(currentProduct);
		}
	}

	private void OnDisable()
	{
	}

	public void SetPrice(Product product)
	{
		currentProduct = product;
		if (product is ProductVehicleUpgrade)
		{
			ProductFreeUpgrade vehicleFreeUpgrade = MarketController.Instance.GetVehicleFreeUpgrade(product as ProductVehicleUpgrade);
			if (vehicleFreeUpgrade != null && vehicleFreeUpgrade.Count > 0)
			{
				if ((bool)freeLabel)
				{
					freeLabel.SetActive(false);
				}
				if (saleHolder != null)
				{
					saleHolder.SetActive(false);
				}
				if (holder != null)
				{
					holder.SetActive(false);
				}
				return;
			}
		}
		if ((bool)freeLabel)
		{
			freeLabel.SetActive(false);
		}
		if (!forceShowOldStylePrice && product.sale != null && product.SalePercent > 0f)
		{
			SetDiscountPrice(product, Color.white);
		}
		else
		{
			SetPrice(product.PriceIn, product.Price);
		}
	}

	public void SetPrice(MoneyType moneyType, int price)
	{
		switch (moneyType)
		{
		case MoneyType.Cash:
			cashIcon.gameObject.SetActive(true);
			goldIcon.gameObject.SetActive(false);
			break;
		case MoneyType.Gold:
			cashIcon.gameObject.SetActive(false);
			goldIcon.gameObject.SetActive(true);
			break;
		}
		priceLabel.text = price.ToString();
		if (saleHolder != null)
		{
			saleHolder.SetActive(false);
		}
		if (holder != null)
		{
			holder.SetActive(true);
		}
	}

	private void SetDiscountPrice(Product product, Color color)
	{
		try
		{
			saleOldCashIcon.gameObject.SetActive(false);
			saleOldGoldIcon.gameObject.SetActive(false);
			RedLineSprite.gameObject.SetActive(true);
			saleOldPriceLabel.text = ((product.sale.oldPrice <= 0) ? product.MarketPrice.ToString() : product.sale.oldPrice.ToString());
			saleNewCashIcon.gameObject.SetActive(product.PriceIn == MoneyType.Cash);
			saleNewGoldIcon.gameObject.SetActive(!saleNewCashIcon.gameObject.activeSelf);
			saleNewPriceLabel.text = product.Price.ToString();
			UISprite uISprite = saleNewCashIcon;
			Color color2 = color;
			saleNewPriceLabel.color = color2;
			color2 = color2;
			saleNewGoldIcon.color = color2;
			uISprite.color = color2;
		}
		catch (Exception ex)
		{
			BCWDebug.LogError(ex.Message);
		}
		if (saleHolder != null)
		{
			saleHolder.SetActive(true);
		}
		if (holder != null)
		{
			holder.SetActive(false);
		}
	}

	private bool EnoughMoneyToBuy(Product product)
	{
		return (product.PriceIn.Equals(MoneyType.Cash) && settings.CathedExeptions >= product.Price) || (product.PriceIn.Equals(MoneyType.Gold) && settings.CathedWarnings >= product.Price);
	}
}
